home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / ddj0492.zip / DFLT11.ZIP / BUTTON.C < prev    next >
Text File  |  1992-02-18  |  11KB  |  420 lines

  1. /* -------------- button.c -------------- */
  2.  
  3. #include "dflat.h"
  4.  
  5. int ButtonProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  6. {
  7.     CTLWINDOW *ct = GetControl(wnd);
  8.     int x;
  9.     if (ct != NULL)    {
  10.         switch (msg)    {
  11.             case SETFOCUS:
  12.                 BaseWndProc(BUTTON, wnd, msg, p1, p2);
  13.                 /* ------- fall through ------- */
  14.             case PAINT:
  15.                 if (isVisible(wnd))    {
  16.                     if (TestAttribute(wnd, SHADOW) && cfg.mono == 0)    {
  17.                         /* -------- draw the button's shadow ------- */
  18.                         background = WndBackground(GetParent(wnd));
  19.                         foreground = BLACK;
  20.                         for (x = 1; x <= WindowWidth(wnd); x++)
  21.                             wputch(wnd, 223, x, 1);
  22.                         wputch(wnd, 220, WindowWidth(wnd), 0);
  23.                     }
  24.                     if (ct->itext != NULL)    {
  25.                         unsigned char *txt;
  26.                         if ((txt = calloc(1, strlen(ct->itext)+10)) != NULL)    {
  27.                             if (ct->setting == OFF)    {
  28.                                 txt[0] = CHANGECOLOR;
  29.                                 txt[1] = wnd->WindowColors [HILITE_COLOR] [FG] | 0x80;
  30.                                 txt[2] = wnd->WindowColors [STD_COLOR] [BG] | 0x80;
  31.                             }
  32.                             CopyCommand(txt+strlen(txt), ct->itext, !ct->setting,
  33.                                 WndBackground(wnd));
  34.                             SendMessage(wnd, CLEARTEXT, 0, 0);
  35.                             SendMessage(wnd, ADDTEXT, (PARAM) txt, 0);
  36.                             free(txt);
  37.                         }
  38.                     }
  39.                     /* --------- write the button's text ------- */
  40.                     WriteTextLine(wnd, NULL, 0, wnd == inFocus);
  41.                 }
  42.                 return TRUE;
  43.             case KEYBOARD:
  44.                 if (p1 != '\r')
  45.                     break;
  46.                 /* ---- fall through ---- */
  47.             case LEFT_BUTTON:
  48.                 if (cfg.mono == 0)    {
  49.                     /* --------- draw a pushed button -------- */
  50.                     background = WndBackground(GetParent(wnd));
  51.                     foreground = WndBackground(wnd);
  52.                     wputch(wnd, ' ', 0, 0);
  53.                     for (x = 0; x < WindowWidth(wnd); x++)    {
  54.                         wputch(wnd, 220, x+1, 0);
  55.                         wputch(wnd, 223, x+1, 1);
  56.                     }
  57.                 }
  58.                 if (msg == LEFT_BUTTON)
  59.                     SendMessage(NULL, WAITMOUSE, 0, 0);
  60.                 else
  61.                     SendMessage(NULL, WAITKEYBOARD, 0, 0);
  62.                 SendMessage(wnd, PAINT, 0, 0);
  63.                 if (ct->setting == ON)
  64.                     PostMessage(GetParent(wnd), COMMAND, ct->command, 0);
  65.                 else
  66.                     beep();
  67.                 return TRUE;
  68.             case HORIZSCROLL:
  69.                 return TRUE;
  70.             default:
  71.                 break;
  72.         }
  73.     }
  74.     return BaseWndProc(BUTTON, wnd, msg, p1, p2);
  75. }
  76.  
  77. int TextProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  78. {
  79.     CTLWINDOW *ct;
  80.     switch (msg)    {
  81.         case PAINT:
  82.             ct = GetControl(wnd);
  83.             if (ct != NULL && ct->itext != NULL && GetText(wnd) == NULL)    {
  84.                 int i, len;
  85.                 char *cp, *cp2 = ct->itext;
  86.  
  87.                 len = min(ct->dwnd.h, MsgHeight(cp2));
  88.                 cp = cp2;
  89.                 for (i = 0; i < len; i++)    {
  90.                     int mlen;
  91.                     char *txt = cp;
  92.                     char *cp1 = cp;
  93.                     char *np = strchr(cp, '\n');
  94.                     if (np != NULL)
  95.                         *np = '\0';
  96.                     mlen = strlen(cp);
  97.                     while ((cp1 = strchr(cp1, SHORTCUTCHAR)) != NULL)    {
  98.                         mlen += 3;
  99.                         cp1++;
  100.                     }
  101.                     if (np != NULL)
  102.                         *np = '\n';
  103.                     if ((txt = malloc(mlen+1)) != NULL)    {
  104.                          CopyCommand(txt, cp, FALSE, WndBackground(wnd));
  105.                         txt[mlen] = '\0';
  106.                         SendMessage(wnd, ADDTEXT, (PARAM)txt, 0);
  107.                         if ((cp = strchr(cp, '\n')) != NULL)
  108.                             cp++;
  109.                         free(txt);
  110.                     }
  111.                 }
  112.             }
  113.             break;
  114.         default:
  115.             break;
  116.     }
  117.     return BaseWndProc(TEXT, wnd, msg, p1, p2);
  118. }
  119.  
  120. static void SetFocusCursor(WINDOW wnd)
  121. {
  122.     if (wnd == inFocus)    {
  123.         SendMessage(NULL, SHOW_CURSOR, 0, 0);
  124.         SendMessage(wnd, KEYBOARD_CURSOR, 1, 0);
  125.     }
  126. }
  127.  
  128. int RadioButtonProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  129. {
  130.     int rtn;
  131.     DBOX *db = GetParent(wnd)->extension;
  132.     CTLWINDOW *ct = GetControl(wnd);
  133.     if (ct != NULL)    {
  134.         switch (msg)    {
  135.             case SETFOCUS:
  136.                 if (!(int)p1)
  137.                     SendMessage(NULL, HIDE_CURSOR, 0, 0);
  138.             case MOVE:
  139.                 rtn = BaseWndProc(RADIOBUTTON, wnd, msg, p1, p2);
  140.                 SetFocusCursor(wnd);
  141.                 return rtn;
  142.             case PAINT:    {
  143.                 char rb[] = "( )";
  144.                 if (ct->setting)
  145.                     rb[1] = 7;
  146.                 SendMessage(wnd, CLEARTEXT, 0, 0);
  147.                 SendMessage(wnd, ADDTEXT, (PARAM) rb, 0);
  148.                 SetFocusCursor(wnd);
  149.                 break;
  150.             }
  151.             case KEYBOARD:
  152.                 if ((int)p1 != ' ')
  153.                     break;
  154.             case LEFT_BUTTON:
  155.                 PushRadioButton(db, ct->command);
  156.                 break;
  157.             default:
  158.                 break;
  159.         }
  160.     }
  161.     return BaseWndProc(RADIOBUTTON, wnd, msg, p1, p2);
  162. }
  163.  
  164. int CheckBoxProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  165. {
  166.     int rtn;
  167.     CTLWINDOW *ct = GetControl(wnd);
  168.     if (ct != NULL)    {
  169.         switch (msg)    {
  170.             case SETFOCUS:
  171.                 if (!(int)p1)
  172.                     SendMessage(NULL, HIDE_CURSOR, 0, 0);
  173.             case MOVE:
  174.                 rtn = BaseWndProc(CHECKBOX, wnd, msg, p1, p2);
  175.                 SetFocusCursor(wnd);
  176.                 return rtn;
  177.             case PAINT:    {
  178.                 char cb[] = "[ ]";
  179.                 if (ct->setting)
  180.                     cb[1] = 'X';
  181.                 SendMessage(wnd, CLEARTEXT, 0, 0);
  182.                 SendMessage(wnd, ADDTEXT, (PARAM) cb, 0);
  183.                 SetFocusCursor(wnd);
  184.                 break;
  185.             }
  186.             case KEYBOARD:
  187.                 if ((int)p1 != ' ')
  188.                     break;
  189.             case LEFT_BUTTON:
  190.                 ct->setting ^= ON;
  191.                 SendMessage(wnd, PAINT, 0, 0);
  192.                 return TRUE;
  193.             default:
  194.                 break;
  195.         }
  196.     }
  197.     return BaseWndProc(CHECKBOX, wnd, msg, p1, p2);
  198. }
  199.  
  200. int BoxProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  201. {
  202.     int rtn;
  203.     CTLWINDOW *ct = GetControl(wnd);
  204.     if (ct != NULL)    {
  205.         switch (msg)    {
  206.             case SETFOCUS:
  207.             case PAINT:
  208.                 return FALSE;
  209.             case LEFT_BUTTON:
  210.             case BUTTON_RELEASED:
  211.                 return SendMessage(GetParent(wnd), msg, p1, p2);
  212.             case BORDER:
  213.                 rtn = BaseWndProc(BOX, wnd, msg, p1, p2);
  214.                 if (ct != NULL)
  215.                     if (ct->itext != NULL)
  216.                         writeline(wnd, ct->itext, 1, 0, FALSE);
  217.                 return rtn;
  218.             default:
  219.                 break;
  220.         }
  221.     }
  222.     return BaseWndProc(BOX, wnd, msg, p1, p2);
  223. }
  224.  
  225. int SpinButtonProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  226. {
  227.     int rtn;
  228.     CTLWINDOW *ct = GetControl(wnd);
  229.     if (ct != NULL)    {
  230.         switch (msg)    {
  231.             case CREATE_WINDOW:
  232.                 wnd->wd -= 2;
  233.                 wnd->rc.rt -= 2;
  234.                 break;
  235.             case SETFOCUS:
  236.                 rtn = BaseWndProc(SPINBUTTON, wnd, msg, p1, p2);
  237.                 if (!(int)p1)
  238.                     SendMessage(NULL, HIDE_CURSOR, 0, 0);
  239.                 SetFocusCursor(wnd);
  240.                 return rtn;
  241.             case PAINT:
  242.                 foreground = FrameForeground(wnd);
  243.                 background = FrameBackground(wnd);
  244.                 wputch(wnd, UPSCROLLBOX, WindowWidth(wnd), 0);
  245.                 wputch(wnd, DOWNSCROLLBOX, WindowWidth(wnd)+1, 0);
  246.                 SetFocusCursor(wnd);
  247.                 break;
  248.             case LEFT_BUTTON:
  249.                 if (p1 == GetRight(wnd) + 1)
  250.                     SendMessage(wnd, KEYBOARD, UP, 0);
  251.                 else if (p1 == GetRight(wnd) + 2)
  252.                     SendMessage(wnd, KEYBOARD, DN, 0);
  253.                 if (wnd != inFocus)
  254.                     SendMessage(wnd, SETFOCUS, TRUE, 0);
  255.                 return TRUE;
  256.             case LB_SETSELECTION:
  257.                 rtn = BaseWndProc(SPINBUTTON, wnd, msg, p1, p2);
  258.                 wnd->wtop = (int) p1;
  259.                 SendMessage(wnd, PAINT, 0, 0);
  260.                 return rtn;
  261.             default:
  262.                 break;
  263.         }
  264.     }
  265.     return BaseWndProc(SPINBUTTON, wnd, msg, p1, p2);
  266. }
  267.  
  268. static int WatchIconProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  269. {
  270.     int rtn;
  271.     switch (msg)    {
  272.         case CREATE_WINDOW:
  273.             rtn = DefaultWndProc(wnd, msg, p1, p2);
  274.             SendMessage(wnd, CAPTURE_MOUSE, 0, 0);
  275.             SendMessage(wnd, HIDE_MOUSE, 0, 0);
  276.             SendMessage(wnd, CAPTURE_KEYBOARD, 0, 0);
  277.             return rtn;
  278.         case PAINT:
  279.             SetStandardColor(wnd);
  280.             writeline(wnd, " └ ", 1, 1, FALSE);
  281.             return TRUE;
  282.         case BORDER:
  283.             rtn = DefaultWndProc(wnd, msg, p1, p2);
  284.             writeline(wnd, "═", 2, 0, FALSE);
  285.             return rtn;
  286.         case MOUSE_MOVED:
  287.             SendMessage(wnd, MOVE, p1, p2);
  288.             return TRUE;
  289.         case CLOSE_WINDOW:
  290.             SendMessage(wnd, RELEASE_MOUSE, 0, 0);
  291.             SendMessage(wnd, RELEASE_KEYBOARD, 0, 0);
  292.             SendMessage(wnd, SHOW_MOUSE, 0, 0);
  293.             break;
  294.         default:
  295.             break;
  296.     }
  297.     return DefaultWndProc(wnd, msg, p1, p2);
  298. }
  299.  
  300. WINDOW WatchIcon(void)
  301. {
  302.     int mx, my;
  303.     WINDOW wnd;
  304.     SendMessage(NULL, CURRENT_MOUSE_CURSOR, (PARAM) &mx, (PARAM) &my);
  305.     wnd = CreateWindow(
  306.                     BOX,
  307.                     NULL,
  308.                     mx, my, 3, 5,
  309.                     NULL,NULL,
  310.                     WatchIconProc,
  311.                     VISIBLE | HASBORDER | SHADOW | SAVESELF);
  312.     return wnd;
  313. }
  314.  
  315. static int (*GenericProc)(WINDOW wnd,MESSAGE msg,PARAM p1,PARAM p2);
  316. static BOOL KeepRunning;
  317. static int SliderLen;
  318. static int Percent;
  319. extern DBOX SliderBoxDB;
  320.  
  321. static void InsertPercent(char *s)
  322. {
  323.     int offset;
  324.     char pcc[5];
  325.  
  326.     sprintf(s, "%c%c%c",
  327.             CHANGECOLOR,
  328.             color[DIALOG][SELECT_COLOR][FG]+0x80,
  329.             color[DIALOG][SELECT_COLOR][BG]+0x80);
  330.     s += 3;
  331.     memset(s, ' ', SliderLen);
  332.     *(s+SliderLen) = '\0';
  333.     sprintf(pcc, "%d%%", Percent);
  334.     strncpy(s+SliderLen/2-1, pcc, strlen(pcc));
  335.     offset = (SliderLen * Percent) / 100;
  336.     memmove(s+offset+4, s+offset, strlen(s+offset)+1);
  337.     sprintf(pcc, "%c%c%c%c",
  338.             RESETCOLOR,
  339.             CHANGECOLOR,
  340.             color[DIALOG][SELECT_COLOR][BG]+0x80,
  341.             color[DIALOG][SELECT_COLOR][FG]+0x80);
  342.     strncpy(s+offset, pcc, 4);
  343.     *(s + strlen(s) - 1) = RESETCOLOR;
  344. }
  345.  
  346. static int SliderTextProc(WINDOW wnd,MESSAGE msg,PARAM p1,PARAM p2)
  347. {
  348.     switch (msg)    {
  349.         case PAINT:
  350.             Percent = (int)p2;
  351.             InsertPercent(GetText(wnd) ?
  352.                 GetText(wnd) : SliderBoxDB.ctl[1].itext);
  353.             GenericProc(wnd, PAINT, 0, 0);
  354.             if (Percent >= 100)
  355.                 SendMessage(GetParent(wnd),COMMAND,ID_CANCEL,0);
  356.             if (!dispatch_message())
  357.                 PostMessage(GetParent(wnd), ENDDIALOG, 0, 0);
  358.             return KeepRunning;
  359.         default:
  360.             break;
  361.     }
  362.     return GenericProc(wnd, msg, p1, p2);
  363. }
  364.  
  365. static int SliderBoxProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  366. {
  367.     int rtn;
  368.     WINDOW twnd;
  369.     switch (msg)    {
  370.         case CREATE_WINDOW:
  371.             AddAttribute(wnd, SAVESELF);
  372.             rtn = DefaultWndProc(wnd, msg, p1, p2);
  373.             twnd = SliderBoxDB.ctl[1].wnd;
  374.             GenericProc = twnd->wndproc;
  375.             twnd->wndproc = SliderTextProc;
  376.             KeepRunning = TRUE;
  377.             SendMessage(wnd, CAPTURE_MOUSE, 0, 0);
  378.             SendMessage(wnd, CAPTURE_KEYBOARD, 0, 0);
  379.             return rtn;
  380.         case COMMAND:
  381.             if ((int)p2 == 0 && (int)p1 == ID_CANCEL)    {
  382.                 if (Percent >= 100 ||
  383.                         YesNoBox("Terminate process?"))
  384.                     KeepRunning = FALSE;
  385.                 else
  386.                     return TRUE;
  387.             }
  388.             break;
  389.         case CLOSE_WINDOW:
  390.             SendMessage(wnd, RELEASE_MOUSE, 0, 0);
  391.             SendMessage(wnd, RELEASE_KEYBOARD, 0, 0);
  392.             break;
  393.         default:
  394.             break;
  395.     }
  396.     return DefaultWndProc(wnd, msg, p1, p2);
  397. }
  398.  
  399. WINDOW SliderBox(int len, char *ttl, char *msg)
  400. {
  401.     SliderLen = len;
  402.     SliderBoxDB.dwnd.title = ttl;
  403.     SliderBoxDB.dwnd.w =
  404.         max(strlen(ttl),max(len, strlen(msg)))+4;
  405.     SliderBoxDB.ctl[0].itext = msg;
  406.     SliderBoxDB.ctl[0].dwnd.w = strlen(msg);
  407.     SliderBoxDB.ctl[0].dwnd.x =
  408.         (SliderBoxDB.dwnd.w - strlen(msg)-1) / 2;
  409.     SliderBoxDB.ctl[1].itext =
  410.         realloc(SliderBoxDB.ctl[1].itext, len+10);
  411.     Percent = 0;
  412.     InsertPercent(SliderBoxDB.ctl[1].itext);
  413.     SliderBoxDB.ctl[1].dwnd.w = len;
  414.     SliderBoxDB.ctl[1].dwnd.x = (SliderBoxDB.dwnd.w-len-1)/2;
  415.     SliderBoxDB.ctl[2].dwnd.x = (SliderBoxDB.dwnd.w-10)/2;
  416.     DialogBox(NULL, &SliderBoxDB, FALSE, SliderBoxProc);
  417.     return SliderBoxDB.ctl[1].wnd;
  418. }
  419.  
  420.